﻿using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class OrderTrackingDAL : BaseDAL<OrderTrackingDAL>
	{
		#region 默认方法
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(OrderTracking model)
		{
			var strSql=new StringBuilder();
			strSql.Append("insert into OrderTracking(");			
            strSql.Append("OrderId,OptDesc,OptTime,PreOrderStatus,OrderStatus,OptAccountId,OptConsumerId,Longitude,Latitude,IpAddress,IsDelete");
			strSql.Append(") values (");
            strSql.Append("@OrderId,@OptDesc,@OptTime,@PreOrderStatus,@OrderStatus,@OptAccountId,@OptConsumerId,@Longitude,@Latitude,@IpAddress,@IsDelete");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@OptDesc", SqlDbType.NVarChar,300) ,            
                        new SqlParameter("@OptTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@PreOrderStatus", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderStatus", SqlDbType.Int,4) ,            
                        new SqlParameter("@OptAccountId", SqlDbType.Int,4) ,            
                        new SqlParameter("@OptConsumerId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Longitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@Latitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@IpAddress", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };
			            
            parameters[0].Value = model.OrderId;                        
            parameters[1].Value = model.OptDesc;                        
            parameters[2].Value = model.OptTime;                        
            parameters[3].Value = model.PreOrderStatus;                        
            parameters[4].Value = model.OrderStatus;                        
            parameters[5].Value = model.OptAccountId;                        
            parameters[6].Value = model.OptConsumerId;                        
            parameters[7].Value = model.Longitude;                        
            parameters[8].Value = model.Latitude;                        
            parameters[9].Value = model.IpAddress;                        
            parameters[10].Value = model.IsDelete;            			
            var obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			return obj == null ? 0 : Convert.ToInt32(obj);
		}
				
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(OrderTracking model)
		{
			var strSql=new StringBuilder();
			strSql.Append("update OrderTracking set ");
			                                                
            strSql.Append(" OrderId = @OrderId , ");                                    
            strSql.Append(" OptDesc = @OptDesc , ");                                    
            strSql.Append(" OptTime = @OptTime , ");                                    
            strSql.Append(" PreOrderStatus = @PreOrderStatus , ");                                    
            strSql.Append(" OrderStatus = @OrderStatus , ");                                    
            strSql.Append(" OptAccountId = @OptAccountId , ");                                    
            strSql.Append(" OptConsumerId = @OptConsumerId , ");                                    
            strSql.Append(" Longitude = @Longitude , ");                                    
            strSql.Append(" Latitude = @Latitude , ");                                    
            strSql.Append(" IpAddress = @IpAddress , ");                                    
            strSql.Append(" IsDelete = @IsDelete  ");            			
			strSql.Append(" where Id=@Id ");
						
			SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@OptDesc", SqlDbType.NVarChar,300) ,            
                        new SqlParameter("@OptTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@PreOrderStatus", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderStatus", SqlDbType.Int,4) ,            
                        new SqlParameter("@OptAccountId", SqlDbType.Int,4) ,            
                        new SqlParameter("@OptConsumerId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Longitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@Latitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@IpAddress", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };
						            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.OrderId;                        
            parameters[2].Value = model.OptDesc;                        
            parameters[3].Value = model.OptTime;                        
            parameters[4].Value = model.PreOrderStatus;                        
            parameters[5].Value = model.OrderStatus;                        
            parameters[6].Value = model.OptAccountId;                        
            parameters[7].Value = model.OptConsumerId;                        
            parameters[8].Value = model.Longitude;                        
            parameters[9].Value = model.Latitude;                        
            parameters[10].Value = model.IpAddress;                        
            parameters[11].Value = model.IsDelete;                        
            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
		}
				
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			var strSql=new StringBuilder();
			strSql.Append("update OrderTracking set IsDelete=1 ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			var strSql=new StringBuilder();
			strSql.Append("update OrderTracking set IsDelete=1 ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
		}
						
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DataSet GetModel(int Id)
		{
			var strSql=new StringBuilder();
			strSql.Append("select Id, OrderId, OptDesc, OptTime, PreOrderStatus, OrderStatus, OptAccountId, OptConsumerId, Longitude, Latitude, IpAddress, IsDelete  ");			
			strSql.Append("  from OrderTracking ");
			strSql.Append(" where Id=@Id  and IsDelete=0");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;
			
			var ds = DbHelperSQL.Query(strSql.ToString(),parameters);			
			return ds.Tables[0].Rows.Count>0 ? ds : null;
		}
				
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			var strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM OrderTracking where IsDelete=0 ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				strSql.Append(" and "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}		
		#endregion
		
		#region 扩展方法
        /// <summary>
        /// 通过订单ID获取订单轨迹列表
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public List<OrderTracking> GetOrderTrackByOrderId(int orderId)
        {
            var sbSql = new StringBuilder();
            sbSql.Append("select Id, OrderId, OptDesc, OptTime, PreOrderStatus, OrderStatus, OptAccountId, ");
            sbSql.Append("OptConsumerId, Longitude, Latitude, IpAddress, IsDelete  ");
            sbSql.Append("FROM OrderTracking ");
            sbSql.Append("WHERE OrderId=@OrderId  and IsDelete=0");
            sbSql.Append("ORDER BY OptTime DESC ");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderId", SqlDbType.Int,4)
			};
            parameters[0].Value = orderId;

            var lsRet = DbHelperSQL.ExecuteSqlList<OrderTracking>(sbSql.ToString(), parameters);
            return lsRet != null && lsRet.Count > 0 ? lsRet.ToList() : null;
        }

        /// <summary>
        /// 通过订单ID以及前置状态、后置状态 查询是否有相同订单轨迹记录
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="preOrderStatus"></param>
        /// <param name="orderStatus"></param>
        /// <returns></returns>
	    public int CheckExsitOrderSameTracking(int orderId, int preOrderStatus, int orderStatus)
	    {
            var sbSql = new StringBuilder();
            sbSql.Append("select COUNT(Id) FROM OrderTracking ");
            sbSql.Append(
                "WHERE OrderId=@OrderId AND PreOrderStatus=@PreOrderStatus AND OrderStatus=@OrderStatus AND IsDelete=0");
            sbSql.Append("ORDER BY OptTime DESC ");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderId", SqlDbType.Int,4),
                    new SqlParameter("@PreOrderStatus", SqlDbType.Int,4),
                    new SqlParameter("@OrderStatus", SqlDbType.Int,4)
			};
            parameters[0].Value = orderId;
            parameters[1].Value = preOrderStatus;
            parameters[2].Value = orderStatus;

            var nRet = DbHelperSQL.GetSingle(sbSql.ToString(), parameters);
            return nRet == null ? 0 : (int) nRet;
	    }

		#endregion
	}
}
